local joy__moyu = fk.CreateSkill {

  name = "joy__moyu",

  tags = {  },

}



joy__moyu:addEffect(fk.EventPhaseStart, {
  name = "joy__moyu",
  events = {fk.EventPhaseStart},
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(joy__moyu.name) and target == player and player.phase == Player.Play
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, joy__moyu.name, nil, "#joy__moyu-invoke")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn, 2)
    room:addPlayerMark(player, "joy__moyu_draw")
    room:addPlayerMark(player, "@@joy__moyu-turn")
    room:broadcastProperty(player, "MaxCards")
  end,
})

joy__moyu:addEffect(fk.TurnEnd, {
  name = "#joy__moyu_delay",
  mute = true,
  events = {fk.TurnEnd, fk.DrawNCards},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      return target == player and player:isWounded() and player:getMark("@@joy__moyu-turn") > 0
    else
      return target == player and player:getMark("joy__moyu_draw-turn") > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      room:recover { num = math.min(player.maxHp-player.hp,player:getMark("@@joy__moyu-turn")), skillName = "joy__moyu", who = player, recoverBy = player }
    else
      data.n = data.n + 2 * player:getMark("joy__moyu_draw-turn")
    end
  end,
})
joy__moyu:addEffect(fk.DrawNCards, {
  name = "#joy__moyu_delay",
  mute = true,
  events = {fk.TurnEnd, fk.DrawNCards},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnEnd then
      return target == player and player:isWounded() and player:getMark("@@joy__moyu-turn") > 0
    else
      return target == player and player:getMark("joy__moyu_draw-turn") > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnEnd then
      room:recover { num = math.min(player.maxHp-player.hp,player:getMark("@@joy__moyu-turn")), skillName = "joy__moyu", who = player, recoverBy = player }
    else
      data.n = data.n + 2 * player:getMark("joy__moyu_draw-turn")
    end
  end,
})
joy__moyu:addEffect(fk.TurnStart, {
  refresh_events = {fk.TurnStart},
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("joy__moyu_draw") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "joy__moyu_draw-turn", player:getMark("joy__moyu_draw"))
    player.room:setPlayerMark(player, "joy__moyu_draw", 0)
  end,
})

joy__moyu:addEffect("prohibit", {
  name = "#joy__moyu_prohibit",
  is_prohibited = function(self, from, to, card)
    return from:getMark("@@joy__moyu-turn") > 0 and from ~= to
  end,
})

return joy__moyu